/*
@title: Virtual_Machine
@description: Virtual Machine is a game where you can navigate different pages of a virtual machine. It's just designed to be fun - no worries about trying to improve your score!
@author: N_Rizwan
@tags: ["strategy" , "maze"]
@addedOn: 2022-09-08
*/

//===================VARIABLES=====================    
const player = "p";
const w = "w";
const w1 = "r";
const w2 = "s";
const w3 = "f";
const w4 = "q";
const w5 = "t";
const w6 = "y";
const w7 = "u";
const folder = "d";
const dustbin = "k";
const skull = "l";
const power = "o";
const poweroff = "g";
const black = "h";
const back = "b";
const box = "M";
const portal = "P";
const bg = tune`
12151.898734177215`;
playTune(bg, Infinity);

//==============BITMAPS===================

setLegend(
  [ player, bitmap`
................
..00.....000....
.0220..001220...
.0222001201220..
.0122220122220..
..012222222L220.
...012222L22L20.
...0012222L2220.
...0LL122222220.
...0L11222222020
....011222222220
.....0111102210.
......00001210..
........0L110...
........0LL0....
.........00.....`],
  [ w, bitmap`
0000000000000000
1111111111111111
1111111111111111
0000000000000000
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222`],
  [ w1, bitmap`
0110222222222222
0110222222222222
0110222222222222
0110222222222222
0110222222222222
0110222222222222
0110222222222222
0110222222222222
0110222222222222
0110222222222222
0110222222222222
0110222222222222
0110222222222222
0110222222222222
0110222222222222
0110222222222222`],
  [ w2, bitmap`
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
0000000000000000
1111111111111111
1111111111111111
0000000000000000`],
  [ w3, bitmap`
2222222222220110
2222222222220110
2222222222220110
2222222222220110
2222222222220110
2222222222220110
2222222222220110
2222222222220110
2222222222220110
2222222222220110
2222222222220110
2222222222220110
2222222222220110
2222222222220110
2222222222220110
2222222222220110`],
  [ w4, bitmap`
2222200000000000
2222001111111111
2220011111111111
2200111000000000
2001110022222222
0011100222222222
0111002222222222
0110022222222222
0110222222222222
0110222222222222
0110222222222222
0110222222222222
0110222222222222
0110222222222222
0110222222222222
0110222222222222`],
  [ w5, bitmap`
0110222222222222
0110222222222222
0110222222222222
0110222222222222
0110222222222222
0110222222222222
0110222222222222
0110222222222222
0110022222222222
0111002222222222
0011100222222222
2001110022222222
2200111000000000
2220011111111111
2222001111111111
2222200000000000`],
  [ w6, bitmap`
2222222222220110
2222222222220110
2222222222220110
2222222222220110
2222222222220110
2222222222220110
2222222222220110
2222222222220110
2222222222200110
2222222222001110
2222222220011100
2222222200111002
0000000001110022
1111111111100222
1111111111002222
0000000000022222`],
  [ w7, bitmap`
0000000000222222
1111111110022222
1111111111002222
0000000011100222
2222222001110022
2222222200111002
2222222220011100
2222222222001110
2222222222200110
2222222222220110
2222222222220110
2222222222220110
2222222222220110
2222222222220110
2222222222220110
2222222222220110`],
  [ folder, bitmap`
................
................
...0000000......
..022222220000..
.02666666622220.
0266666000000000
0266660222222260
0200002666666660
0202226666666660
0202666666666660
0202666666666660
0202666666666660
0602666666666660
0602666666666660
.06666666666660.
..000000000000..`],
  [ dustbin, bitmap`
......0000......
....000.L000....
...0.....LLL0...
..0...11.....0..
..011111111110..
...0.11.11.10...
...0.11.11.10...
...0.11.11.10...
...0.11.11.10...
...0.11.11.10...
...0.11.11.10...
...0.LL.LL.L0...
...0........0...
...01......10...
....00000000....
................`],
  [ skull, bitmap`
................
...0000000000...
..002222222200..
.00222222222200.
.02222222222220.
.02222222222220.
.00002220000220.
.00002220000220.
.000220200022.0.
.02222222222000.
.02222222220000.
.0022222200000..
..02022020000...
..0000000000....
................
................`],
  [ power, bitmap`
2333333333333332
3333333223333333
3333333223333333
3333223223223333
3332223223222333
3332233223322333
3322333223332233
3322333333332233
3322333333332233
3312233333322333
3312223333222333
3331222222221333
3333112222113333
3333311111133333
3333333333333333
2333333333333332`],
  [ poweroff, bitmap`
2444444444444442
4444444444444444
4444444442222444
4444224442224444
4442224442222444
4442244442122244
4422444444412244
4422444444412244
4422444444412244
4412244444122L44
4412224444222L44
444122222222L444
4444112222LL4444
4444411111L44444
4444444444444444
2444444444444442`],
  [ back, bitmap`
2222000000002222
2200222222220022
2022220222222202
2022200222222202
0222000222222220
0220000000222220
0200000000002220
0220000000000220
0222000220000220
0222200222000220
0222220222000220
0222222222000220
2022222222202202
2022222222222202
2200222222220022
2222000000002222`],
  [black, bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`],
  [box, bitmap`
2222222222222222
2002222222000222
0550222222052022
0551000000155202
0551LL2222155520
0551LL2L22155520
0551LL2222155520
0555111111555520
0555555555555520
0555111111155520
0551666666615520
0551611166615520
0551666666615520
0351611111615320
0551LLLLLLL15520
2000000000000002`],
  [portal, bitmap`
................
................
.....0000000....
....00LLLLL0....
...00LL000000...
...0L00828880...
..00L02228880...
..01L028888880..
..010288888880..
..010288888280..
.0110888888280..
.0108888888880..
.0108888888880..
.01088888828800.
001088882228880.
010888888888880.`],
);

//====================LEVELS=========================
let level = 0;
const levels = [
  map`
qwwwwwwwwwwwu
rk...h.d.h.of
rhh.hh.h.P..f
r..M..ghghhhf
r.hhh.....hbf
r.h...h.hd..f
rpP.h.hMh..lf
tsssssssssssy`, //MAIN PAGE
  map`
qwwwwwwwwwwwu
r.........f.f
ggggggggg.f.f
r.........f.f
r.....p...f.f
r.........f.f
r...........f
tsssssssssssy`, //FOLDER
  map`
qwwwwwwwwwwwu
r.P..d....P.f
r.hh.....dh.f
r....d.P..d.f
r..d..phh...f
rPhh..hh.dP.f
rhhP......hhf
tsssssssssssy`, //TRASH
  map`
qwwwwwwwwwwwu
rp..........f
r.hh.hh.hP..f
r.h...M.hh.hf
r.hhhhh....hf
r...........f
r.hhhhh...hhf
tsssssssssssy`, //HACK
  map`
qwwwwwwwwwwwu
rqw.qu.ty.wuf
rtf.rw.fr.ryf
r.f.......r.f
r....qwu...pf
rssssrgfwwwwf
r....tsy....f
tsssssssssssy`,//LOG OUT
  
];
const currentLevel = levels[level];
setMap(currentLevel);

//==================PROPERTIES===================

setSolids([player,w,w1,w2,w3,w4,w5,w6,w7,black,box,folder,back,poweroff]);
setPushables({
  [player]: [box,folder,back,poweroff],
  [box]: [box],
 
});

//===============PLAYER INPUT===================

onInput("s", () => {getFirst(player).y += 1});
onInput("w", () => {getFirst(player).y -= 1});
onInput("d", () => {getFirst(player).x += 1});
onInput("a", () => {getFirst(player).x -= 1});

//======================HEADER============================
addText("-Virtual Machine-", { color: color`8` });
addText("-L to go back-", { y:15, color: color`8` });
//========================================================


//========================================================


afterInput(() => {
  //=================DUSTBIN====================

  
  const check2 = tilesWith(dustbin, player).length;
  if (check2 > 0) {
    level = 2

    // console.log("Load trash");
    
    const currentLevel = levels[level];
    if (currentLevel !== "") {
      setMap(currentLevel);
      return;
    }
  }

  //=================SKULL====================

  
  const check3 = tilesWith(skull, player).length;
  if (check3 > 0) {
    level = 3
    const currentLevel = levels[level];
    if (currentLevel !== "") {
      setMap(currentLevel);
      addText("-HACKED-", { y: 4, color: color`0` });
      return;
    } 
  }

  //=================POWER====================

  const check4 = tilesWith(power, player).length;
  if (check4 > 0) {
    level = 4 

    const currentLevel = levels[level];

    if (currentLevel !== "") {
      setMap(currentLevel);
      addText(" LOGGED OUT ", { y: 7, color: color`0` });
      return;
    }
  }
  //===================================================================================
});


//============PRESS `l` To go back to MAIN PAGE==================
onInput("l", () => {
  const currentLevel = levels[level=0];
  clearText();
  setMap(currentLevel);
});
